package com.bst.etl.datax.web;

import com.bst.common.annotation.Log;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.page.TableDataInfo;
import com.bst.common.enums.BusinessType;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.etl.datax.entity.JobTemplate;
import com.bst.etl.datax.entity.JobTemplateTask;
import com.bst.etl.datax.mapper.JobTemplateMapper;
import com.bst.etl.datax.service.JobTemplateTaskService;
import com.bst.etl.domain.EtlDrawBaseDatax;
import com.bst.etl.domain.EtlDrawDm;
import com.bst.etl.domain.EtlDrawDw;
import com.bst.etl.service.IEtlDrawBaseDataxService;
import com.bst.etl.service.IEtlDrawDwService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-02-17
 */
@Controller
@RequestMapping("/base/job/template/task")
public class JobTemplateTaskController extends BaseController
{
    private String prefix = "etl/job/template/task";

    @Autowired
    private JobTemplateTaskService jobTemplateTaskService;
    @Autowired
    private JobTemplateMapper jobTemplateMapper;
    @Autowired
    IEtlDrawBaseDataxService etlDrawBaseDataxService;

    @GetMapping()
    public String task(Integer idTemplate, ModelMap mmap)
    {
        JobTemplate template = jobTemplateMapper.loadById(idTemplate);
        mmap.put("template",template);
        return prefix + "/templateTask";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobTemplateTask jobTemplateTask)
    {
        startPage();
        List<JobTemplateTask> list = jobTemplateTaskService.selectJobTemplateTaskList(jobTemplateTask);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JobTemplateTask jobTemplateTask)
    {
        List<JobTemplateTask> list = jobTemplateTaskService.selectJobTemplateTaskList(jobTemplateTask);
        ExcelUtil<JobTemplateTask> util = new ExcelUtil<JobTemplateTask>(JobTemplateTask.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    @ResponseBody
    public AjaxResult addSave(@RequestBody List<JobTemplateTask> list)
    {
        return toAjax(jobTemplateTaskService.insertBatchJobTemplateTask(list));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{idTemplateTask}")
    public String edit(@PathVariable("idTemplateTask") Long idTemplateTask, ModelMap mmap)
    {
        JobTemplateTask jobTemplateTask = jobTemplateTaskService.selectJobTemplateTaskByIdTemplateTask(idTemplateTask);
        mmap.put("jobTemplateTask", jobTemplateTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JobTemplateTask jobTemplateTask)
    {
        return toAjax(jobTemplateTaskService.updateJobTemplateTask(jobTemplateTask));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jobTemplateTaskService.deleteJobTemplateTaskByIdTemplateTasks(ids));
    }

    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove2")
    @ResponseBody
    public AjaxResult remove(@RequestBody JobTemplateTask task)
    {
        return toAjax(jobTemplateTaskService.deleteJobTemplateTaskByTemplateTask(task));
    }

    @Log(title = "基础任务添加", businessType = BusinessType.QUERY)
    @GetMapping("/toAddDatax")
    public String toAddDatax(Long templateId, ModelMap mmap) {
        mmap.put("templateId",templateId);
        return prefix+"/addDatax";
    }

    @Log(title = "数仓任务添加", businessType = BusinessType.QUERY)
    @GetMapping("/toAddDw")
    public String toAddDw(Long templateId, ModelMap mmap) {
        mmap.put("templateId",templateId);
        return prefix+"/addDw";
    }

    @Log(title = "数据集市添加", businessType = BusinessType.QUERY)
    @GetMapping("/toAddDm")
    public String toAddDm(Long templateId, ModelMap mmap) {
        mmap.put("templateId",templateId);
        return prefix+"/addDm";
    }
    /**
     * 查询datax列表
     */
    @PostMapping("/datax/list")
    @ResponseBody
    public TableDataInfo dataxList(Long templateId,Long idDb)
    {
        EtlDrawBaseDatax datax = new EtlDrawBaseDatax();
        datax.setFgAct(1);
        datax.setIdDb(idDb);
        List<EtlDrawBaseDatax> list = etlDrawBaseDataxService.selectEtlDrawBaseDataxList(datax);
        JobTemplateTask task = new JobTemplateTask();
        task.setEuType("datax");
        task.setIdTemplate(templateId);
        List<JobTemplateTask> templateTaskList = jobTemplateTaskService.selectJobTemplateTaskList(task);
        List<Long> dataxIds = templateTaskList.stream().map(JobTemplateTask::getIdFk).collect(Collectors.toList());
        List<EtlDrawBaseDatax> newList = new ArrayList<>();
        for (EtlDrawBaseDatax etlDrawBaseDatax : list) {
            if(dataxIds.contains(etlDrawBaseDatax.getIdDrawDatax())) {
                continue;
            }
            newList.add(etlDrawBaseDatax);
        }
        return getDataTable(newList);
    }

    @Autowired
    IEtlDrawDwService etlDrawDwService;

    @PostMapping("/dw/list")
    @ResponseBody
    public TableDataInfo dwList(Long templateId)
    {
        EtlDrawDw dw = new EtlDrawDw();
        dw.setFgAct(1);
        List<EtlDrawDw> list = etlDrawDwService.selectEtlDrawDwList(dw);
        JobTemplateTask task = new JobTemplateTask();
        task.setEuType("dw");
        task.setIdTemplate(templateId);
        List<JobTemplateTask> templateTaskList = jobTemplateTaskService.selectJobTemplateTaskList(task);
        List<Long> dataxIds = templateTaskList.stream().map(JobTemplateTask::getIdFk).collect(Collectors.toList());
        List<EtlDrawDw> newList = new ArrayList<>();
        for (EtlDrawDw d : list) {
            if(dataxIds.contains(d.getIdDw())) {
                continue;
            }
            newList.add(d);
        }
        return getDataTable(newList);
    }
//
//    @PostMapping("/dm/list")
//    @ResponseBody
//    public TableDataInfo dmList(Long templateId)
//    {
//        EtlDrawDm dm = new EtlDrawDm();
//        dm.setFgAct(1);dm.setEuStatus(1);
//        List<EtlDrawDm> list = etlDrawDmService.selectEtlDrawDmList(dm);
//        JobTemplateTask task = new JobTemplateTask();
//        task.setEuType("dm");
//        task.setIdTemplate(templateId);
//        List<JobTemplateTask> templateTaskList = jobTemplateTaskService.selectJobTemplateTaskList(task);
//        List<Long> dataxIds = templateTaskList.stream().map(JobTemplateTask::getIdFk).collect(Collectors.toList());
//        List<EtlDrawDm> newList = new ArrayList<>();
//        for (EtlDrawDm d : list) {
//            if(dataxIds.contains(d.getIdDrawDm())) {
//                continue;
//            }
//            newList.add(d);
//        }
//        return getDataTable(newList);
//    }
}
